<?php
$mypage="rex_lang";
$Basedir = dirname(__FILE__);
$entry_id = !empty($entry_id) ? (int) $entry_id : 0;
$mode = !empty($mode) ? (string) $mode: ''; 
$func = !empty($func) ? (string) $func: ''; 

if(($func == 'delete')&&($entry_id!="")) 
{
 
	$qry = 'DELETE FROM rex_393_domain WHERE id='. $entry_id;
  
	$sql = new rex_sql();
	$sql->setQuery($qry);
	$func = ''; 
}

if (($func == 'edit' || $func == "add") && !isset($_POST['rexform_delete_button']) && ($entry_id != "" || $_POST['s1_language'] != "") && ($_POST['s1_is_main']=='1') && (isset($_POST['s1_language']))) {
	$sql = new rex_sql();
	$sql->setQuery("update rex_393_domain set is_main=0 where language=" . (int) $_POST['s1_language'] . " and is_main=1");
	unset($sql);
}


if ($func == '')
{
	require_once $Basedir.'/../../addon_framework/classes/list/class.rex_list.inc.php';

	/* create domains list */
	$sql = 'select * from rex_393_domain';
  
	$list = new rexlist($sql, 'id', 'asc');
	$list->setLabel($I18N_393->msg('label_domain_list'));
//	$list->debug = true;

$cols = array();
	$col = new resultColumn('domain', $I18N_393->msg('label_domain'));
	$col->setParams(array ('func' => 'edit', 'entry_id' => '%id%'));
	$cols[] = $col;
	$col = new resultColumn('is_main', $I18N_393->msg('label_main_domain'));
	$col->addCondition('is_main', '0', ' '); 
	$col->addCondition('is_main', '1', 'ja'); 
	$col->delOption(OPT_SORT, OPT_SEARCH);
	$cols[] = $col;	
	$col = new resultColumn('language', $I18N_393->msg('label_language'));
	foreach ($REX['CLANG'] as $lang_id => $lang)
		$col->addCondition('language', $lang_id, htmlspecialchars($lang));
	$cols[] = $col;
  
   	for ($i=0;$i<sizeof($cols);$i++)
		$list->addColumn($cols[$i]); 
   
	$browseBar = new browseBar();
	$browseBar->setAddButtonStatus(true);
	$list->addToolbar($browseBar, 'top', 'half');
	$list->addToolbar(new searchBar(), 'top', 'half');
	$list->addToolbar(new statusBar(), 'bottom', 'half');
	$list->addToolbar(new maxElementsBar(), 'bottom', 'half');
	$list->setStepping(25);

	$list->show(false);
}
elseif ($func == 'edit' || $func == 'add')
{
	require_once $Basedir.'/../../addon_framework/classes/form/class.rex_form.inc.php';

	$form = & new rexForm('structure_form');
	$form->setApplyUrl('index.php?page='.$mypage.'&subpage=domain');
	$form->setEditMode($entry_id != '');
 
//  $form->debug = true;


	//------------------------------> Hidden Fields
	$fieldFunc = & new hiddenField('func');
	$fieldFunc->setValue('edit');

	$fieldEntryId = & new hiddenField('entry_id');
	$fieldEntryId->setValue($entry_id);

	//------------------------------> Fields[Allgemein]

	$fields = array();

	$field = & new textField('domain', $I18N_393->msg('label_domain'));
	$field->addValidator('notEmpty', $I18N_393->msg('miss_domain'));
	$fields[]=$field;
  
	$field = & new selectField('is_main', $I18N_393->msg('label_main_domain'));
	$field->addOption('nein','0');
	$field->addOption('ja','1');
	$field->addAttribute('size','1');
	$fields[]=$field;
    
	$field = & new selectField('language', $I18N_393->msg('label_language'));
	foreach ($REX['CLANG'] as $lang_id => $lang)
		$field->addOption(htmlspecialchars($lang),$lang_id);
	$field->addAttribute('size','1');
	$fields[]=$field;
  
	$sectionCommon = & new rexFormSection('rex_393_domain', $I18N_393->msg('label_language_form'), array ('id' => $entry_id));
  
	for($i = 0; $i < count($fields); $i++)
		$sectionCommon->addField($fields[$i]);
 

	$form->addSection($sectionCommon);

	//------------------------------> Add Fields: Form
	$form->addField($fieldFunc);
	$form->addField($fieldEntryId);
	
	$form->show(true);  
  
}
?>